#!/usr/bin/env ruby

require 'json'

def output_error(error_msg, error_key)
  return if error_msg.nil?

  if error_msg.is_a? Array
    error_msg.each do |i|
      error_id = common_error_id i
      puts "error.[#{error_key}].#{error_id}: 1"
      puts "error.#{error_id}.message: #{@ansible_failed_info}"
    end
  elsif error_msg.is_a? String
    error_id = common_error_id error_msg
    puts "error.[#{error_key}].#{error_id}: 1"
    puts "error.#{error_id}.message: #{@ansible_failed_info}"
  end
end

def common_error_id(line)
  line.gsub!(/[^\w]/, '-')
  line.gsub!(/-+/, '-') # Replace multiple consecutive '-' with a single one
  line.gsub!(/^-|-$/, '')
  line
end

while (line = STDIN.gets)
  next unless line =~ /(FAILED!|failed:).*=>(.*)/

  @ansible_failed_info = $2
  next if @ansible_failed_info.empty?

  ansible_failed_json = JSON.parse @ansible_failed_info

  output_error ansible_failed_json['msg'],'msg'
  output_error ansible_failed_json['message'],'message'
  output_error ansible_failed_json['cmd'],'cmd'
  output_error ansible_failed_json['failures'],'failures'
end
